﻿#include "chatlistviewer.h"
#include <QMenu>
#include "characterset.h"
#include "chatlistmodel.h"
#include "chatcontent.h"

ChatListViewer::ChatListViewer(QWidget *parent) : QListView(parent)
{
    setModel(new ChatListModel(this));
    setItemDelegate(new ChatListDelegate(this));
    connect(model(), &QAbstractItemModel::rowsInserted, this, &QAbstractItemView::scrollToBottom, Qt::QueuedConnection);
}

int ChatListViewer::showChat(bool fromSelf, const QPixmap &icon, const QString &name, const QString &content, qint64 timestamp)
{
    if(auto model = qobject_cast<ChatListModel*>(this->model()))
    {
        auto text = new ChatDirection(!fromSelf, icon, content, fromSelf ? QString() : name);
        int row =  model->appendContent(text, timestamp);
        auto index = model->index(row);
        openPersistentEditor(index);
        auto idx = model->index(row);
        emit dataChanged(idx, idx);
        return row;
    }
    return -1;
}

int ChatListViewer::showImage(const QPixmap &icon, const QString &/*name*/, const QUrl &/*resourceName*/, const QPixmap &pixmap)
{
    if(auto model = qobject_cast<ChatListModel*>(this->model()))
    {
        auto image = new ChatImage(icon, pixmap);
        int row =  model->appendContent(image);
        auto index = model->index(row);
        openPersistentEditor(index);
        auto idx = model->index(row);
        emit dataChanged(idx, idx);
        return row;
    }
    return -1;
}

int ChatListViewer::showImage(bool fromSelf, const QPixmap &icon, const QString &name, const QUrl &url, qint64 timestamp)
{
    if(auto model = qobject_cast<ChatListModel*>(this->model()))
    {
        bool requestDownload = false;
        auto image = new ChatImage(!fromSelf, icon, url, requestDownload, fromSelf ? QString() : name);
        int row = model->appendContent(image, timestamp);
        auto index = model->index(row);
        openPersistentEditor(index);
        auto idx = model->index(row);
        emit dataChanged(idx, idx);
        if(requestDownload)
            model->downloadRequested(image->getUrl());
        return row;
    }
    return -1;
}

void ChatListViewer::initMid(const QByteArray &mid, int row, const QString &text)
{
    if(auto model = qobject_cast<ChatListModel*>(this->model()))
    {
        if(auto content = model->getContent(row))
        {
            if(auto dir = dynamic_cast<ChatDirection*>(content))
            {
                dir->mid = mid;
                if(!dir->from)  //我发的
                {
                    //空mid不显示状态
                    QString extra = mid.isEmpty()? QString() : text;
                    if(dir->extraText != extra)
                    {
                        dir->extraText = extra;
                        auto index = model->index(row);
                        dataChanged(index, index);
                    }
                }
            }
        }
    }
}

void ChatListViewer::setMidState(const QByteArray &mid, const QString &text)
{
    if(auto model = qobject_cast<ChatListModel*>(this->model()))
    {
        auto result = model->findDirection(mid);
        if(result.second && !result.second->from && result.second->extraText != text)
        {
            result.second->extraText = text;
            auto index = model->index(result.first);
            dataChanged(index, index);
        }
    }
}

bool ChatListViewer::hasMid(const QByteArray &mid) const
{
    if(auto model = qobject_cast<ChatListModel*>(this->model()))
    {
        auto result = model->findDirection(mid);
        return result.second;
    }
    return false;
}

void ChatListViewer::append(const QString &text)
{
    //添加一段普通文字
    if(auto model = qobject_cast<ChatListModel*>(this->model()))
    {
        model->appendContent(new ChatPlainText(text));
        emit appended();
    }
}

void ChatListViewer::clearAll()
{
    //清空
    if(auto model = qobject_cast<ChatListModel*>(this->model()))
    {
        model->clearAll();
        emit cleared();
    }
}

void ChatListViewer::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu *menu = Q_NULLPTR;

    if(model()->rowCount() > 0)
    {
        if(!menu)
            menu = new QMenu(this);
        //清空
        menu->addAction(QObject::trECSUTF8("清空"), this, &ChatListViewer::clearAll);
    }
    if(menu)
    {
        menu->setAttribute(Qt::WA_DeleteOnClose);
        menu->popup(event->globalPos());
    }
}

void ChatListViewer::resizeEvent(QResizeEvent *event)
{
    if(event->size().width() == event->oldSize().width())
        QListView::resizeEvent(event);
    else
        doItemsLayout();
}
